package main

import (
	"bufio"
	"io"
	"log"
	"net"
	"os"
)

func init() {
	log.SetFlags(log.Lshortfile | log.Lmicroseconds)
}

func send(conn net.Conn) {
	for {
		reader := bufio.NewReader(os.Stdin)
		line, _, _ := reader.ReadLine()
		if string(line) == "EXIT" {
			log.Println("[退出]")
			conn.Close()
			return
		}
		conn.Write(line)
	}
}

func main() {

	conn, err := net.Dial("tcp", "47.52.77.233:8989")
	if err != nil {
		log.Panicln(err)
	}
	defer conn.Close()

	go send(conn)
	//等着收消息
	buf := make([]byte, 1024)

	for {
		n, err := conn.Read(buf)
		if err == io.EOF || err != nil {
			log.Println(err)
			break
		}
		log.Println("[receive]", string(buf[:n]))
	}

}
